package com.ruiysoft.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.crm.mapper.BizBatchClientRecordMapper;
import com.ruiysoft.crm.mapper.BizClientMapper;
import com.ruiysoft.crm.model.entity.BizBatchClientRecord;
import com.ruiysoft.crm.model.entity.BizClient;
import com.ruiysoft.crm.model.entity.SysUser;
import com.ruiysoft.crm.model.vo.BizClientVo;
import com.ruiysoft.crm.service.BizClientService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author fanyl
 */
@Service
public class BizClientServiceImpl extends ServiceImpl<BizClientMapper, BizClient> implements BizClientService {
    private final BizClientMapper bizClientMapper;
    private final BizBatchClientRecordMapper bizBatchClientRecordMapper;

    public BizClientServiceImpl(BizClientMapper bizClientMapper, BizBatchClientRecordMapper bizBatchClientRecordMapper) {
        this.bizClientMapper = bizClientMapper;
        this.bizBatchClientRecordMapper = bizBatchClientRecordMapper;
    }

    @Override
    public IPage queryClientInfo(IPage page, Map<String, Object> params) {
        params.remove("page");
        params.remove("pageSize");
        params.remove("isAsc");
        String successValue = (String) params.get("success");
        String chudy = (String) params.get("chudy");
        String allocation = (String) params.get("allocation");//是否已分配
        if (allocation != null) {
            params.remove("allocation");
            if ("1".equals(allocation)) { //已分配
                params.put("allocation", " client.user_name is not null");
            }
            if ("0".equals(allocation)) { //已分配
                params.put("allocation", " client.user_name is null");
            }
        }
        if ("1".equals(chudy)) {
            params.remove("client.user_id");
        }
        params.remove("chudy");
        if ("-1".equals(successValue)) {
            params.put("success", "IFNULL(client.success,'-1') = '-1' ");
        } else {
            params.put("success", "IFNULL(client.success,'') in ('" + successValue + "')");
        }
        List<BizClient> result = bizClientMapper.queryClientAndCarInfo(page, params);
        result.forEach(bizClient -> {
            if (StringUtils.hasText(bizClient.getIdNo())) {
                bizClient.setIdNo(bizClient.getIdNo().replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1*****$2"));
            }

            if (StringUtils.hasText(bizClient.getMobilePhone())) {
                bizClient.setMobilePhone(bizClient.getMobilePhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            }

            if (StringUtils.hasText(bizClient.getStandbyMobile())) {
                bizClient.setStandbyMobile(bizClient.getStandbyMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            }
        });


        return page.setRecords(result);
    }

    @Override
    public BizClient getClientById(int id) {
        return bizClientMapper.getClientById(id);
    }

    @Override
    public Object recv_center(int i) {
        return null;
    }

    @Override
    public boolean revokeAllotUser(BizBatchClientRecord record) {
        try {
            String clientIds = record.getClientIds();
            if (StringUtils.isEmpty(clientIds)) {
                return false;
            }
            bizClientMapper.revokeAllotUser(clientIds);
            record.setUserId(null);
            record.setBatchType("import");
            record.setUserName(null);
            bizBatchClientRecordMapper.deleteById(record.getId());
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Object allocationByBatchid(String batchid, SysUser userVO) {
        BizBatchClientRecord bbc = bizBatchClientRecordMapper.selectById(batchid);
        bizClientMapper.allocationByBatchid(batchid, userVO.getUserId(), userVO.getRealName(), bbc.getClientIds());
        bbc.setUserId(userVO.getUserId());
        bbc.setBatchType("allot");
        bbc.setUserName(userVO.getRealName());
        bizBatchClientRecordMapper.updateById(bbc);
        return 1;
    }


    @Override
    public List<BizClientVo> queryClients(Map<String, Object> params) {
        return bizClientMapper.queryClients(params);
    }

    @Override
    public List<String> getCarNos() {
        return bizClientMapper.getCarNos();
    }
}